package com.xidian.zhiancloud.controller.chemicalsManagement.dangerousPart;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.DangerousChemicalStorage;
import com.xidian.zhiancloud.qo.chemicalsManagement.DangerousChemicalStorageQO;
import com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.dangerousPart.DangerousChemicalStorageService;
import com.xidian.zhiancloud.vo.chemicalsManagement.DangerousChemicalInfoVO;
import com.xidian.zhiancloud.vo.chemicalsManagement.DangerousChemicalStorageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 危化品管理 危化品库存管理 危化品入库管理
 *
 * @Author: Egoist
 * @Date: 2020/11/10 17:31
 */
@RestController
@RequestMapping("/DangerousChemicalStorage")
public class DangerousChemicalStorageController extends BaseController {

    @Autowired
    private DangerousChemicalStorageService dangerousChemicalStorageService;

    /**
     * 增加危化品入库记录
     *
     * @param dangerousChemicalStorage 增加危化品入库记录对象
     * @return 返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(DangerousChemicalStorage dangerousChemicalStorage) {

        dangerousChemicalStorage.setCreateTime(new Date(System.currentTimeMillis()));
        dangerousChemicalStorage.setCompanyId(getSessionUserCompanyId());

        return ResultDTO.successOf(dangerousChemicalStorageService.save(dangerousChemicalStorage), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 对已有危化品入库信息进行更新，否则新增记录
     *
     * @param dangerousChemicalStorage 增加危化品入库记录对象
     * @return 返回是否添加成功
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    @PostMapping("/addOrUpdate")
    public ResultDTO<Boolean> addOrUpdate(DangerousChemicalStorage dangerousChemicalStorage) {
        dangerousChemicalStorage.setCreateTime(new Date(System.currentTimeMillis()));
        dangerousChemicalStorage.setCompanyId(getSessionUserCompanyId());
        Long storageId = getSessionUserid();
        return ResultDTO.successOf(dangerousChemicalStorageService.addOrUpdate(dangerousChemicalStorage, storageId), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获取危化品入库记录
     *
     * @param id 危化品入库记录对象的id
     * @return 危化品入库记录
     */
    @GetMapping("/getStorageVOById")
    public ResultDTO<DangerousChemicalStorageVO> getStorageVOById(Long id) {
        return ResultDTO.successOf(dangerousChemicalStorageService.getStorageVOById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获取危化品入库详情基本信息页
     *
     * @param id 危化品入库记录对象的id
     * @return 危化品入库详情基本信息页
     */
    @GetMapping("/getById")
    public ResultDTO<DangerousChemicalInfoVO> getChemicalInfoById(Long id) {
        return ResultDTO.successOf(dangerousChemicalStorageService.getInfoById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获取危化品出入库记录
     *
     * @param id 危化品出入库记录对象的id
     * @return 危化品入库记录
     */
    @GetMapping("/getRecordById")
    public ResultDTO<List<Map>> getRecordById(Long id) {
        Long companyId = getSessionUserCompanyId();
        return ResultDTO.successOf(dangerousChemicalStorageService.getRecordById(id, companyId), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除危化品入库记录
     *
     * @param ids ids数组
     * @return 返回是否删除成功
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {

        return ResultDTO.successOf(dangerousChemicalStorageService.removeByIds(Arrays.asList(ids)), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改危化品入库记录对象
     *
     * @param dangerousChemicalStorage 危化品入库记录
     * @return 返回是否修改成功
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(DangerousChemicalStorage dangerousChemicalStorage) {
        dangerousChemicalStorage.setCreateTime(null);
        dangerousChemicalStorage.setCompanyId(null);
        return ResultDTO.successOf(dangerousChemicalStorageService.updateById(dangerousChemicalStorage), GeneralResponseEnums.SUCCESS);
    }


    /**
     * 列表查询
     *
     * @param dangerousChemicalStorageQO 请求参数
     * @return 返回入库信息等列表
     */
    @GetMapping("/getListByQueryObject")
    public ResultDTO<IPage<DangerousChemicalStorageVO>> getListByQueryObject(DangerousChemicalStorageQO dangerousChemicalStorageQO) {
        if (dangerousChemicalStorageQO.getCurrent() == null || dangerousChemicalStorageQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        dangerousChemicalStorageQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerousChemicalStorageService.getListByQueryObject(dangerousChemicalStorageQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 获得危化品类型列表
     * @return 危化品类型列表
     */
    @GetMapping("/getTypeList")
    public ResultDTO<Map<Long, String>> getTypeList() {
        Long companyId = getSessionUserCompanyId();
        return ResultDTO.successOf(dangerousChemicalStorageService.getTypeList(companyId), GeneralResponseEnums.SUCCESS);
    }

}
